package com.score;


import android.os.Environment;

import java.io.File;

/**
 * 文件目录管理器,通过此类的静态方法获取文件目录及文件路径
 */
public class DirManager {
    //app私有内存存储路径
    private static final String DIR_APP = BaseApplication.getInstance().getExternalFilesDir(null).getAbsolutePath();
//    {@link android.os.Environment#DIRECTORY_MUSIC},
//            *            {@link android.os.Environment#DIRECTORY_PODCASTS},
//            *            {@link android.os.Environment#DIRECTORY_RINGTONES},
//            *            {@link android.os.Environment#DIRECTORY_ALARMS},
//            *            {@link android.os.Environment#DIRECTORY_NOTIFICATIONS},
//            *            {@link android.os.Environment#DIRECTORY_PICTURES}, or
//     *            {@link android.os.Environment#DIRECTORY_MOVIES}.
    private static final String DIR_IMAGE = Environment.DIRECTORY_PICTURES;
    private static final String DIR_LOG = "log";
    private static final String DIR_AUDIO = "audio";
    private static final String DIR_VIDEO = "video";
    private static final String DIR_CRASH = "crash";
    private static final String DIR_OTHER = "other";
    private static final String DIR_RECODER = "recoder";

    public static String getAppDir() {
        return DIR_APP;
    }

    public static String getImageDir() {
        File dir = new File(getAppDir() + "/" + DIR_IMAGE);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getImagePath(String imageName) {
        return getImageDir() + "/" + imageName;
    }

    public static String getAudioDir() {
        File dir = new File(getAppDir() + "/" + DIR_AUDIO);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getAudioPath(String audioName) {
        return getAudioDir() + "/" + audioName;
    }

    public static String getRecoderDir() {
        File dir = new File(getAppDir() + "/" + DIR_RECODER);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getRecoderPath(String recoderName) {
        return getRecoderDir() + "/" + recoderName;
    }

    public static String getVideoDir() {
        File dir = new File(getAppDir() + "/" + DIR_VIDEO);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getVideoPath(String videoName) {
        return getVideoDir() + "/" + videoName;
    }

    public static String getLogDir() {
        File dir = new File(getAppDir() + "/" + DIR_LOG);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getLogPath(String logName) {
        return getLogDir() + "/" + logName;
    }

    public static String getCrashDir() {
        File dir = new File(getAppDir() + "/" + DIR_CRASH);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getCrashPath(String crashName) {
        return getCrashDir() + "/" + crashName;
    }

    public static String getOtherDir() {
        File dir = new File(getAppDir() + "/" + DIR_OTHER);
        if (!dir.exists())
            dir.mkdirs();
        return dir.getAbsolutePath();
    }

    public static String getOtherPath(String otherName) {
        return getOtherDir() + "/" + otherName;
    }


    /**
     * 清空目录
     */
    public static boolean clearDirs() {
        boolean res = deleteDir(getAppDir());
        return res;
    }

    /**
     * 删除整个文件目录及子文件
     */
    public static boolean deleteDir(String dirPath) {
        File dirFile = new File(dirPath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        File newFile = new File(dirFile.getAbsolutePath() + System.currentTimeMillis());
        dirFile.renameTo(newFile);// 重命名之后再删除，防止出现异常

        File[] files = newFile.listFiles();
        int i = 0;
        for (; i < files.length; i++) {
            if (files[i].isFile()) {
                if (false == files[i].delete())
                    break;
            } else {
                if (!deleteDir(files[i].getAbsolutePath()))
                    break;
            }
        }
        if (i < files.length)
            return false;
        return newFile.delete();
    }

    /**
     * 返回目录下包含的文件和目录的总数目
     *
     * @param dirPath
     * @return
     */
    public static int getFileNum(String dirPath) {
        File dirFile = new File(dirPath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return 0;
        }
        return dirFile.listFiles().length;
    }

    public static long getFilesSpace() {
       return getFilesSpace(getAppDir());
    }
        /**
         * 返回目录下所有文件的大小总和（多少个字节）
         *
         * @param dirPath
         * @return 所有文件的大小总和(单位字节)
         */
    public static long getFilesSpace(String dirPath) {
        File dirFile = new File(dirPath);
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return 0;
        }
        int totalSpace = 0;
        File[] files = dirFile.listFiles();
        if (files == null)
            return 0;
        for (int i = 0; i < files.length; i++) {
            if (files[i].isFile()) {
                totalSpace += files[i].length();
            } else {
                totalSpace += getFilesSpace(files[i].getAbsolutePath());
            }
        }
        return totalSpace;
    }
}
